'use strict';

const Controller = require('../core/base_controller');

class LoginController extends Controller {
  async index() {
    const { ctx, app } = this;
    const result = await ctx.service.login.todo(ctx.request.body);
    if (result.length === 0) {
      this.fail('用户名不存在!');
      return;
    }
    console.log(result[0].password === ctx.request.body.password);
    if (result[0].password === ctx.request.body.password) {
      // 创建token
      const token = app.jwt.sign(result[0], app.config.jwt.secret, {
        expiresIn: 60 * 60 * 24, // 设置过期时间
      });
      // json格式返回token
      this.success(token);
    } else {
      this.fail('密码错误!');
    }

    // ctx.body = result;
  }
}

module.exports = LoginController;
